/* SharePoint Console Project
 * Http://SharePointConsole.codeplex.com
 * Copyright Jean-Alain Baeyens (2009)
 * Software under Microsoft Reciprocal License (Ms-RL)
 */

using System;
using System.Xml;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;
using Sharepoint.Console.Commands;

namespace Sharepoint.Console.List
{
    public class ChangeDisplayUrl : RecursiveBaseCommande
    {
        public override string ShortHelp
        {
            get
            {
                return "Replace the item display page by another.";
            }
        }

        public override string LongHelp
        {
            get
            {
                return "Replace the page used to display an item of a list by another page.\nThe new page and the list are defined by there url.\nYou can also specify the name of the content type that will be impacted by this operation.";
            }
        }

        public override string Syntax
        {
            get
            {
                return "ChangeDisplayUrl 'ListUrl' 'pageUrl' ['ContentTypeName']";
            }
        }
        
        public override string Execute(string[] args)
        {
            if (args.Length != 4) return string.Format("Invalid number of parameters. Syntax: {0}",Syntax);

            string listUrl = string.Format("{0}/{1}", this.SharepointConnection.Web.Url, args[1]);
            string pageUrl = args[2];

            string contentType = "Item";
            if (args.Length == 4) contentType = args[3];

            SPList list = this.SharepointConnection.Web.GetList(listUrl);
            if (list != null)
            {
                try
                {
                    list.ContentTypes[contentType].DisplayFormUrl = pageUrl;
                    list.ContentTypes[contentType].Update();
                    return "Display form updated";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
            else
            {
                return "Invalid list";
            }
        }
    }
}

